<?php
/**
 * BaseObject
 * - Provide common functions for reader
 * @author Do Truong Giang
 */
class BaseObject {
	protected $errors;
	protected $warning;
	
	/**
	 * Constructor
	 */
	public function BaseObject() {
		$this->errors = array();
		$this->warnings = array();
	}
	/**
	 * underScoreToUpperCase
	 * - Convert a underscore string to camel case
	 * - Eg: abc_xyz becomes abcXyz
	 */
	protected function underScoreToUpperCase($str) {
		$str = strtolower($str);
		$length = strlen($str);
		for ($i = 0; $i < $length; $i++) {
			if ($str{$i} === "_") {
				$str{$i + 1} = strtoupper($str{$i + 1});
			}
		}
		$str = str_replace("_", "", $str);
		return $str;
	}
	/**
	 * camelFormat
	 * - Underscores are changed to camel format, including the first character
	 */
	protected function camelFormat($str) {
		$str = self::underScoreToUpperCase($str);
		$str{0} = strtoupper($str{0});
		return $str;
	}
	private function readTemplate($url, $params) {
		$handle = fopen($url, "r");
		$code = fread($handle, filesize($url));
		foreach ($params as $key => $value) {
			$code = str_replace("%$key%", $value, $code);
		}
		return $code;
	}
	/**
	 * storeDbSourceCode
	 * - Store the source code to Db file
	 */
	protected function storeDbSourceCode($srcCode, $fileName) {
		// Store to output folder
		$outputFile = OUTPUT_FOLDER_DB . "/base/Base$fileName" . ".class.php";
		$handle = fopen($outputFile, "w");
		fwrite($handle, $srcCode);
		fclose($handle);
		// Write the extends class
		$outputFile = OUTPUT_FOLDER_DB . "/$fileName" . ".class.php";
		if (!is_file($outputFile)) {
			$params = array("CLASS_NAME" => $fileName);
			$code = $this->readTemplate(DOCUMENT_ROOT . "/codegen/templates/dao_extends.txt", $params);
			$handle = fopen($outputFile, "w");
			fwrite($handle, $code);
			fclose($handle);
		}
	}
	/**
	 * storeUiSourceCode
	 * - Store the source code to Ui file
	 */
	protected function storeUiSourceCode($srcCode, $fileName) {
		// Store to output folder
		$outputFile = OUTPUT_FOLDER_UI . "/base/Base$fileName" . ".class.php";
		$handle = fopen($outputFile, "w");
		fwrite($handle, $srcCode);
		fclose($handle);
		// Store the extends class
		$outputFile = OUTPUT_FOLDER_UI . "/$fileName" . ".class.php";
		if (!is_file($outputFile)) {
			$params = array("CLASS_NAME" => $fileName);
			$code = $this->readTemplate(DOCUMENT_ROOT . "/codegen/templates/ui_extends.txt", $params);
			$handle = fopen($outputFile, "w");
			fwrite($handle, $code);
			fclose($handle);
		}
	}
	/**
	 * hasError
	 * - Return true if has error, otherwise return false
	 */
	public function hasError() {
		if (empty($this->errors)) {
			return false;
		}
		return true;
	}
	/**
	 * showError
	 * - Show the errors to screen
	 */
	public function showError() {
		echo "<h1>ERROR</h1>";
		foreach ($this->errors as $error) {
			echo $error . "<br>";
		}
		echo "<hr><h1>Warning</h1>";
		foreach ($this->warnings as $warning) {
			echo $warning . "<br>";
		}
	}
	/**
	 * addError
	 * - Add an error to an error array
	 * @param String $message The error message
	 * @return void
	 */
	public function addError($message) {
		$this->errors[] = $message;
	}
	/**
	 * addWarning
	 * - Add a warning to warning array
	 */
	public function addWarning($message) {
		$this->warnings[] = $message;
	}
}
?>